##Chapter7Master.r
###master script for creating all Tables and Figures 
###in original analysis included in Chapter 7 of:
###Enos, Ryan D.  2017. The Space Between Us:
### Social Geography and Politics
####RdE December 2017

rm(list = ls())

library(apsrtable)
library(Zelig)
library(ggplot2)

##clustered standard errors function
cl   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  uj = uj[complete.cases(uj),]  
  
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  return(vcovCL) }

##master formulas for all scripts below
master.formula = 'outgroup.dissim.yeshiva*outgroup.yeshiva+demo1.sex+demo1.age+demo1.ethnicity+demo2.left_right+income+high.school+immigrant+nonjewish_pcnt+jerusalem'
master.formula.interactions = 'outgroup.dissim.yeshiva*outgroup.yeshiva+demo1.sex+demo1.age+demo1.ethnicity+demo2.left_right+income+high.school+immigrant+nonjewish_pcnt+jerusalem+high.outgroup.interactions'
master.formula.reduced = 'outgroup.dissim.yeshiva*outgroup.yeshiva'


##names of coefficients for regression table output below
coef.names = c('Intercept', 'Segregation', 'Outgroup Proportion',  'Segregation x Outgroup Proportion', 'Male', 'Age', 'Mixed Ethnicity', 'Other Ethnicity', 'Sephardic Ethnicity', 'Political Ideology','High Income','Low Income','High School','Immigrant','Arab Population','Jerusalem','Contact')


source('TableA26.r')
source('TableA27.r')
source('TableA28.r')
source('TableA29Figure7_4.r')
source('TableA30.r')